#include "general.h"
#if (SENSOR_TYPE == Sensor_OV7710)
#include "pv_api.h"
#include "sensor_cdsp.h"
#include "sp1k_aaa_api.h"
#define HAL_FRONT_REMOVE_UNCALL_FUNCTION

UINT8	RawR[256],RawG[256],RawB[256];

#define MAX_FRAME_RATE_30_IDX 1	// New limited for OV3630 - Jim@0424
#define MIN_FRAME_RATE_30_IDX 1	// New limited for OV3630 - Jim@0424
#define MAX_FRAME_RATE_15_IDX 1	// New limited for OV3630 - Jim@0424
#define MIN_FRAME_RATE_15_IDX 1	// New limited for OV3630 - Jim@0424

code AEstr LVtbl60[11];
		

#define MAX_FRAME_RATE_25_IDX 1				//Jim@0516 Update 50Hz AE Table
#define MIN_FRAME_RATE_25_IDX 1				//Jim@0516 Update 50Hz AE Table
#define MAX_FRAME_RATE_12P5_IDX 1				//Jim@0516 Update 50Hz AE Table
#define MIN_FRAME_RATE_12P5_IDX 1				//Jim@0516 Update 50Hz AE Table
											
code AEstr LVtbl50[11];


#define MAX_GAIN_IDX (46)
#define MIN_GAIN_IDX 5
code UINT8 gaintbl[61];

#define MCLKMODE        1 /*mclk mode,'1'from I/O Pad,'0'from internal*/
#define PCLKMODE        0 /*pclk mode,'1'from I/O Pad,'0'from internal*/
#define PREV_MCLKDIV    1 /*MCLK div, master clock = (96MHz / mclkdiv)*/
#define PREV_PCLKDIV    1 /*pixel clock = (master clock) / pclkdiv*/
#define SNAP_MCLKDIV    1 /*MCLK div, master clock = (96MHz / mclkdiv)*/
#define SNAP_PCLKDIV    1 /*pixel clock = (master clock) / pclkdiv*/
#if (PCLKMODE == 1)
/* FREQUENCY unity is KHz */
#define PREV_PCLKFREQ (UINT32) 27000000
#define SNAP_PCLKFREQ (UINT32) 27000000
#define CLK_RATIO     (UINT8) ((SNAP_PCLKFREQ/1000) * 128 / (PREV_PCLKFREQ/1000))
#else
#define PREV_PCLKFREQ (UINT32) 96000000 / PREV_MCLKDIV / PREV_PCLKDIV
#define SNAP_PCLKFREQ (UINT32) 96000000 / SNAP_MCLKDIV / SNAP_PCLKDIV
#define CLK_RATIO     (UINT8) ((UINT16) PREV_MCLKDIV * PREV_PCLKDIV * 128 / SNAP_MCLKDIV / SNAP_PCLKDIV)
#endif
#define PCLKphase 0

#define TGGPIOFUNC 0
#define TGGPIODIR 0x04
#define TGGPIOVAL 0

xdata UINT16 exptime;

extern UINT8 HAL_CDSPeofWait( UINT8 Number);

code UINT8 OV7710_Init_tab[] = {
0x12, 0x80,

0x20, 0xcb,
0x11, 0x01,
0x12, 0x20,
0x17, 0x13,
0x18, 0x01,
0x32, 0x60,
0x1b, 0x6a,
0x19, 0x04,
0x1a, 0x7c,
0x47, 0x00,
0x1e, 0x00,
0x09, 0x04,
0x35, 0x00,

0x21, 0x60,
0x2c, 0x80,
0x3d, 0x00,
0x55, 0x40,
0x56, 0x07,
0x43, 0x00,
0x45, 0x00,
0xd8, 0x23,
0xdc, 0x84,
0xe1, 0xc4,
0x5c, 0x40,
0x5e, 0x22,
0x5f, 0x24,
0x60, 0x02,

0x13, 0xa0,
0x00, 0x00,
0x10, 0x00,
0x14, 0x80,
0xbf, 0x58,
0xc0, 0x28,
0xc1, 0x60,
0xc2, 0xf0,
0xc3, 0xd0,
0xc4, 0xd0,
0xc5, 0x31,
0xc6, 0x31,
0x16, 0xc0,
0x48, 0xff,
0x7a, 0x10,
0x5b, 0x91,
0x13, 0xa7,

0x30, 0x40,
0x4a, 0x34,
0x97, 0x01,
0x98, 0x10,
0x99, 0x23,
0x9a, 0x00,
0xa4, 0x04,
0xa5, 0x1c,
0xa6, 0x3a,

0x6d, 0x8f,
0x67, 0x9d,

0x08, 0x08,
0x7b, 0x20,
0x7c, 0x0e,
0x7d, 0x22,
0x7e, 0x3f,
0x7f, 0x63,
0x80, 0x6e,
0x81, 0x77,
0x82, 0x80,
0x83, 0x88,
0x84, 0x90,
0x85, 0x97,
0x86, 0xa4,
0x87, 0xaf,
0x88, 0xc5,
0x89, 0xd7,
0x8a, 0xe8,

0x9b, 0x96,
0x9c, 0x80,
0x9d, 0x16,
0x9e, 0x17,
0x9f, 0x75,
0xa0, 0x8c,
0xFF, 0xFF

};

void HAL_I2C_TBL_Write(
	UINT8 *tab
)
{
	UINT8 i;
	UINT8 reg_addr[1], reg_data[1];

	i = 0;
	reg_addr[0] = reg_data[0] = 0;
	while ( (reg_addr[0] + reg_data[0]) != 0x1fe ) {
		reg_addr[0] = tab[2*i];
		reg_data[0] = tab[2*i + 1];
		HAL_WriteSSC(reg_addr, reg_data, 0x01, 0x00);
		if(i==0) HAL_Wait(100);;
		i++;
		HAL_Wait(1);
	}
}

void  HAL_FrontInit (UINT8 CamMode) USING_0
{
	UINT8 tmp0;
	printf (" Initialize the Front Sensor_OV5620 : Enter !!,CamMode=%d \n",CamMode);

	HAL_SetFrontIOConfig(TGGPIOFUNC, 0x00, TGGPIODIR, TGGPIOVAL);
	HAL_SetFrontCLKSource(MCLKMODE, PCLKMODE);
	HAL_SetFrontCLKSet(PREV_MCLKDIV, PREV_PCLKDIV, 0);
	XBYTE[0x2A50] = 0x02;
	XBYTE[0x2A51] = 0x16;
	tmp0 = XBYTE[0x20E4];
	tmp0 &= 0xF0;
	tmp0 |= PCLKphase;
	XBYTE[0x20E4] |= tmp0;
	HAL_SSCConfig(Sensor_Slave_Address, Sensor_SSC_Freq);
	XBYTE[0x2AB0] = 0x01;		      //Reset Sensor interface
	XBYTE[0x2AB0] = 0x00;
	XBYTE[0x2A50] = 0x02;
	XBYTE[0x2A51] = 0x16;
	XBYTE[0x2A00] = 0x00;
	HAL_SensorRegInit();

	printf (" Initialize the Front : Exit !! \n");
}

void  HAL_FrontPreviewSet(void) USING_0
{
	HAL_FrontVdWait(0,1);
	HAL_SetFrontCLKSource(MCLKMODE, PCLKMODE);

	HAL_SetFrontCLKSet(PREV_MCLKDIV, PREV_PCLKDIV, 0);
	HAL_SetFrontImgCrop(Sensor_Preview_HOffset, Sensor_Preview_VOffset, Sensor_Preview_HSize, Sensor_Preview_VSize, 0x00);
	HAL_SetFrontInverse(Prev_Hsync_Inverse,Prev_Vsync_Inverse);
	HAL_SetFrontReshape(Sensor_Preview_Hfall, Sensor_Preview_Hrise, Sensor_Preview_HReshen, Sensor_Preview_Vfall, Sensor_Preview_Vrise, Sensor_Preview_VReshen);
	HAL_SetSensorPreview();

	XBYTE[0x2110]=Sensor_Preview_BayerPattern;
}

void  HAL_FrontSnapSet(UINT16 Hsize,UINT16 Vsize) USING_0
{

}


void  HAL_SensorRegInit(void)
{
	HAL_I2C_TBL_Write(OV7710_Init_tab);

}



void HAL_SetSensorPreview(void) USING_0
{

}
/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontGainSet                                                     *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control Sensor gain                                                           *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust gain value for sensor                                                     *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, gain value to set                                                    *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/

UINT8 HAL_FrontGainSet(
    UINT32 value,
    UINT32  option
)
{
	return SUCCESS;
}

UINT8 HAL_FrontGainSet_Intr(
    UINT32 value,
    UINT32  option
)USING_1
{
	return SUCCESS;
}

/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontExpTimeSet                                                  *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control exposure time                                                         *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust exposure time value                                                       *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, expossure time value to set, unit 0.1ms                              *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/
UINT32
HAL_FrontExpTimeSet(
	UINT32 value,
	UINT32 option
)
{
	return SUCCESS;
}


UINT32
HAL_FrontExpTimeSet_Intr(
	UINT32 value,
	UINT32 option
) USING_1
{
	return SUCCESS;
}

/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontFrameRateSet                                                *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control Frame Rate                                                            *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust Frame rate for sensor                                                     *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, frame rate value to set                                              *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/

UINT32
HAL_FrontFrameRateSet(
    UINT32 value,
    UINT32 option
)
{
	return 0;
}

UINT32
HAL_FrontFrameRateSet_1(
    UINT32 value,
    UINT32 option
)USING_1
{
	return 0;
}



UINT8 HAL_SnapRaw8(UINT8 mode, UINT8 opt) USING_0
{

}

#ifndef HAL_FRONT_REMOVE_UNCALL_FUNCTION
void HalSensorPrePV(
	void
)
{
	return;
}
#endif

UINT8 HAL_MaxGainIndexGet()
{
	return(MAX_GAIN_IDX);
}

UINT8 HAL_MinGainIndexGet()
{
	return(MIN_GAIN_IDX);
}

UINT8 HAL_MaxExpIndexGet(
UINT16 frame
)
{
	switch (frame) {
		case SP1K_FRAME_RATE_30:
			return(MAX_FRAME_RATE_30_IDX);
			break;
		case SP1K_FRAME_RATE_15:
			return(MAX_FRAME_RATE_15_IDX);
			break;
		case SP1K_FRAME_RATE_25:
			return(MAX_FRAME_RATE_25_IDX);
			break;
		case SP1K_FRAME_RATE_12P5:
			return(MAX_FRAME_RATE_12P5_IDX);
			break;
		default:
			return FAIL;
	};
}

UINT8 HAL_MinExpIndexGet(
UINT16 frame
)
{
	switch (frame) {
		case SP1K_FRAME_RATE_30:
			return(MIN_FRAME_RATE_30_IDX);
			break;
		case SP1K_FRAME_RATE_15:
			return(MIN_FRAME_RATE_15_IDX);
			break;
		case SP1K_FRAME_RATE_25:
			return(MIN_FRAME_RATE_25_IDX);
			break;
		case SP1K_FRAME_RATE_12P5:
			return(MIN_FRAME_RATE_12P5_IDX);
			break;
		default:
			return FAIL;
	};
}

#endif
